﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
namespace ProjectSem3.Admin
{
    public partial class WebForm8 : System.Web.UI.Page
    {
        
        private dataDataContext dc = new dataDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            Employee us = (Employee)Session["user"];
            if(us!=null &&us.RoleEmID==1)
                if (!Page.IsPostBack)
                {
                    if (Request.QueryString["id"] != null)
                    {
                        try
                        {
                            Customer cs = this.dc.Customers.Single(c => c.CustomerID == Convert.ToInt32(Convert.ToString(Request.QueryString["id"])));
                            txtUsername.Text = cs.Username;
                            txtBirthday.Text = cs.BirthDay.Value.ToShortDateString();
                            if (cs.Gender == true && cs.Gender != null)
                                rdoMale.Checked = true;
                            else
                                rdoFemale.Checked = true;
                            this.TextBoxName.Text = cs.Name;
                            this.TextBoxMail.Text = cs.Email;
                            this.TextBoxAddress.Text = cs.Address;
                            this.TextBoxComAddress.Text = cs.CompanyAddress;
                            this.TextBoxPhone.Text = cs.Phone;
                            if (cs.Status == true && cs.Status != null)
                                Radio1.Checked = true;
                            else
                                Radio2.Checked = true;
                        }
                        catch (Exception)
                        {

                            Response.Redirect("Default.aspx");
                        }
                    }
                }
                else {
                    Button1.Visible = false;
                }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var list = dc.Customers;
            Customer Cus = this.dc.Customers.Single(c => c.CustomerID == Convert.ToInt32(Convert.ToString(Request.QueryString["id"])));
            if (Cus.Username.Equals(txtUsername.Text) || list.Where(w => w.Username.Equals(txtUsername.Text)).ToList().Count == 0)
                if (Cus.Email.Equals(TextBoxMail.Text) || list.Where(w => w.Email.Equals(TextBoxMail.Text)).ToList().Count == 0)
                {
                    Cus.Username = txtUsername.Text;
                    Cus.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBoxpassword.Text, "sha1");
                    Cus.Name = this.TextBoxName.Text;
                    Cus.BirthDay = DateTime.Now;
                    Cus.Gender = rdoMale.Checked;
                    Cus.Email = this.TextBoxMail.Text;
                    Cus.Address = this.TextBoxAddress.Text;
                    Cus.CompanyAddress = this.TextBoxComAddress.Text;
                    Cus.Phone = this.TextBoxPhone.Text;
                    Cus.Status = Radio1.Checked;
                    this.dc.SubmitChanges();
                    Response.Redirect("ListCustomer.aspx");
                }
                else
                    lblEmailMessage.Text = "Email is exists";
            else
                LabelMessage.Text = "Username is exists";
        }
    }
}